<?php
/**
 * @title 3040. 相同分数的最大操作数目 II
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function maxOperations($nums) {
        /**
         * @since 2024-02-19 避免重复遍历
         */
        $this->dp = [];

        /**
         * @since 2024-02-19 3种方式最大值
         * @return Integer
         */
        $this->nums = $nums;
        $numCount = count($nums);
        return max(
            $this->find(0, $numCount-1, $nums[0]+$nums[1]),
            $this->find(0, $numCount-1, $nums[0]+$nums[$numCount-1]),
            $this->find(0, $numCount-1, $nums[$numCount-2]+$nums[$numCount-1]),
        );
    }

    /**
     * @title 返回区间内，相同分数的最大操作数目
     * @author start2004
     * @since 2024-02-19 Created
     *
     * @param $s
     * @param $e
     * @param $targetSum
     * @return Integer
     */
    function find($s, $e, $targetSum)
    {
        /**
         * @since 2024-02-19 至少2个元素
         */
        if ($s >= $e) {
            return 0;
        } else {}

        /**
         * @since 2024-02-19 缓存是否命中
         */
        $k = "{$s}-{$e}-{$targetSum}";
        if (isset($this->dp[$k])) {
            return $this->dp[$k];
        } else {}

        /**
         * @since 2024-02-19 3种方式
         */
        if($this->nums[$s] + $this->nums[$s+1] == $targetSum){
            $cnt1 = 1 + $this->find($s+2, $e, $targetSum);
        } else {
            $cnt1 = 0;
        }
        if($this->nums[$s] + $this->nums[$e] == $targetSum){
            $cnt2 = 1 + $this->find($s+1, $e-1, $targetSum);
        } else {
            $cnt2 = 0;
        }
        if($this->nums[$e] + $this->nums[$e-1] == $targetSum){
            $cnt3 = 1 + $this->find($s, $e-2, $targetSum);
        } else {
            $cnt3 = 0;
        }

        /**
         * @return
         */
        return $this->dp[$k] = max($cnt1, $cnt2, $cnt3);
    }
}


/**
 * @url http://127.0.0.1/leetcode/202402/2024.02.19_4.php
 */
$datas = [
    [[3,2,1,2,3,4]], // 3
    [[3,2,6,1,4]], // 2
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->maxOperations(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
